home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / educate / wordy402.zip / XFIND.C < prev    next >
C/C++ Source or Header  |  1995-12-16  |  7KB  |  245 lines

  1. /**************************************************************************/
  2. /*                              XFIND UTILITY                             */
  3. /*                                                                        */
  4. /*                                 M\Cooper                               */
  5. /*                        3425 Chestnut Ridge Rd.                         */
  6. /*                        Grantsville, MD 21536-9801                      */
  7. /*                        --------------------------                      */
  8. /*                        Email:  thegrendel@aol.com                      */
  9. /*                                                                        */
  10. /*                $2.00 to register the entire WORDY package              */
  11. /*                                                                        */
  12. /**************************************************************************/
  13.  
  14. #include <conio.h>
  15. #include "srch.h"
  16. //srch.h (source) is part of the WORDY package
  17.  
  18.  
  19. #define FILE_OPENING_ERROR 3
  20. #define FILENAME_MAXLEN 40
  21. #define CR "\n"
  22. #define FILE_SUFFIX ".fnd"
  23. #define MAXLEN 30
  24. #define LINE_LEN 80
  25. #define NOARGS 1
  26. #define INCREMENT 1
  27. #define SPACE ' '
  28. #define DBLBAR 205
  29. #define WILDCARD '?'
  30. //Wildcard character in command line, may be changed
  31.  
  32. #define BUFFERSIZE 8192
  33.  
  34. char ad[] =
  35. "XFIND utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  36.  
  37.  
  38. void getword( char *lset, char *filename );
  39. void center( char *strng );
  40.  
  41. typedef enum { FALSE, TRUE } Boolean;
  42.  
  43. void main( int argc, char **argv )
  44. {
  45.  
  46.    char letterset [MAXLEN],
  47.         filename [FILENAME_MAXLEN];
  48.  
  49.      if( argc == NOARGS )
  50.         {
  51.         clrscr();
  52.         puts( "Enter a LETTER PATTERN to test [example ab?c??e... " );
  53.         gets( letterset );
  54.      strcpy( filename, "word.lst" );  //default
  55.         }
  56.      else
  57.      if( argc == NOARGS + 1 )
  58.        {
  59.           strcpy( letterset, *(argv + 1) );
  60.        strcpy( filename, "word.lst" ); //default
  61.        }
  62.    else
  63.       {
  64.       strcpy( letterset, *(argv + 1) );
  65.       strcpy( filename, *(argv + 2) );
  66.       }
  67.  
  68.      getword( letterset, filename );
  69. }
  70.  
  71.  
  72. /**********************************WORDTEST********************************/
  73. /*       Function tests if word is constructible from Letterset            */
  74. /*                 Args in: char *letterset, char *word                          */
  75. /*   Returns: error_flag == TRUE (1) if constructible, FALSE (0) if not   */
  76. /**************************************************************************/
  77.  
  78. Boolean wordtest( char *letterset, char *word )
  79. {
  80.     Boolean error_flag;
  81.     static char dup_lset[ MAXLEN ];
  82.     register unsigned int cnt = 0,
  83.                       u,
  84.                       v;
  85.  
  86.      strcpy( dup_lset, letterset );
  87.          
  88.      u = strlen( word );
  89.       v = strlen( letterset );
  90.  
  91.      while( ( *letterset == *word ) || *letterset == WILDCARD )
  92.         {
  93.         letterset++;
  94.         word++;
  95.         cnt++;
  96.         }
  97.  
  98.      if( u <= cnt && u == v )
  99.         error_flag = TRUE;
  100.      else
  101.         error_flag = FALSE;
  102.  
  103.  
  104.         return( error_flag );
  105. }
  106.  
  107. /**************************************************************************/
  108.  
  109. void getword( char *letter_set, char *filename )
  110. {
  111.  
  112.     char    l_set [ MAXLEN ],
  113.         word [ MAXLEN ],
  114.         tempstr [ MAXLEN + 1 ],
  115.         targetfile [ MAXLEN ],
  116.         bar [ LINE_LEN + 1 ],
  117.         double_bar [ LINE_LEN + 1 ],
  118.         messg [7],
  119.   msg2 [16];
  120.  
  121.     FILE *fptr,
  122.         *tfile;
  123.     int fnamelen;
  124.     long wcount = 0L;
  125.  
  126.        memset( bar, '*', LINE_LEN );
  127.        *( bar + LINE_LEN ) = NULL;
  128.        memset( double_bar, DBLBAR, LINE_LEN );
  129.        *( double_bar + LINE_LEN ) = NULL;
  130.  
  131.        /*************opening credits*************/
  132.        clrscr();
  133.        printf( double_bar );
  134.        strcpy( tempstr, ad );
  135.        center ( tempstr );
  136.        printf( tempstr );
  137.        printf( CR );
  138.        printf( double_bar );
  139.        printf( CR );
  140.        /****************************************/
  141.  
  142.  
  143.        strcpy ( l_set, letter_set );
  144.        strcat ( letter_set, CR );
  145.  
  146.        /*   Create name of file to store derived words in   */
  147.        /*********************************************************/
  148.        strcpy( targetfile, "matched.wds" );
  149.        /*********************************************************/
  150.  
  151.        if( !( fptr = fopen( filename, "rt" ) ) )
  152.          {
  153.          printf( "\7\7\7Cannot open word file %s!", filename );
  154.          exit( FILE_OPENING_ERROR );
  155.          }
  156.       if( setvbuf( fptr, NULL, _IOFBF, 2 * BUFFERSIZE ) )
  157.          exit( FILE_OPENING_ERROR + 1 );
  158.  
  159.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  160.          {
  161.          printf( "\7\7\7Cannot open file to save words in!" );
  162.          exit ( FILE_OPENING_ERROR + 1 );
  163.          }
  164.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE ) )
  165.          exit( FILE_OPENING_ERROR + 2 );
  166.  
  167.        /**************'Wait' Message************/
  168.        printf( CR CR );
  169.        printf( "WORKING...\n\n" );
  170.        printf( "This will take a few seconds...\n" );
  171.        printf( "Please be patient.\n\n" );
  172.        printf( "Now searching 100,000+ word file and writing file of valid words.\n\n" );
  173.        /*****************************************/
  174.  
  175.  
  176.  
  177.  
  178.  
  179.        sprintf( tempstr, "Words fitting the pattern of: %s\n", strupr( l_set ) );
  180.        center( tempstr );
  181.        fprintf( tfile, double_bar );
  182.       fprintf( tfile, CR );
  183.        fprintf( tfile, tempstr );
  184.        fprintf( tfile, double_bar );
  185.        fprintf( tfile, CR );
  186.  
  187.  
  188.          /*********************Main Loop*************/     
  189.           while( fgets( word, MAXLEN, fptr ) != NULL )
  190.  
  191.             if( wordtest( letter_set, word ) )
  192.                {
  193.                fprintf( tfile, "%s", word );
  194.                wcount++;
  195.                }
  196.           /*******************************************/
  197.  
  198.       if( wcount == 1 )
  199.          strcpy( msg2, "word fits" );
  200.       else
  201.          strcpy( msg2, "words fit" );
  202.  
  203.           fprintf( tfile, bar );
  204.       fprintf( tfile, CR );
  205.           sprintf( tempstr, "%ld %s the pattern of %s.",
  206.                  wcount, msg2, l_set );
  207.           center( tempstr );              
  208.           fprintf( tfile, tempstr );
  209.           fprintf( tfile, "\n\n" );
  210.  
  211.           center( ad );
  212.           fprintf( tfile, ad );
  213.  
  214.           fcloseall();
  215.  
  216.           if( wcount == INCREMENT )
  217.             strcpy( messg, "word" );
  218.           else
  219.             strcpy( messg, "words" );
  220.           sprintf( tempstr,
  221.                  "The file %s has %ld %s fitting the pattern of %s\7.",
  222.                  targetfile, wcount, messg, l_set );
  223.           center( tempstr );
  224.           printf( CR CR );
  225.           printf( tempstr );
  226.     printf( CR CR );
  227.  
  228. }
  229.  
  230.  
  231.  
  232. void center( char *str )
  233. {
  234.    int padding;
  235.    char st [ LINE_LEN + INCREMENT ];
  236.  
  237.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  238.      memset( st, SPACE, padding );
  239.      *( st + padding ) = NULL;  //Terminate string
  240.      strcat( st, str );
  241.      strcpy( str, st );
  242.  
  243.      return;
  244. }
  245.